package com.aube.plugin.figureex;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.aube.plugin.figure.CharacterActivity;
import com.aube.plugin.figureex.roundedimageview.RoundedImageView;
import com.aube.plugin.model.CharacterEx;
import com.aube.plugin.origin.R;
import com.huyn.baseframework.ImageLoader;
import com.huyn.baseframework.dynamicload.DLBridge;
import com.huyn.baseframework.model.Character;
import com.huyn.baseframework.utils.Utils;
import com.ryg.dynamicload.internal.DLIntent;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * Created by hill on 17/1/3.
 */

public class FigureExGrid extends FrameLayout implements View.OnClickListener {
    private final String TAG = "Aube/FigureExGrid";

    private Context mContext;
    private CharacterEx mCharacterData;
    private DLBridge mBridge;

    private ImageView mFigureImg;
    private RoundedImageView mTeacherImg, mTeacher1Img;
    private TextView mDescText, mTeacherCountTxt;
    private FrameLayout mFigureInfoLy;

    private int mRootWidth, mRootHeight;
    
    public FigureExGrid(Context context) {
        this(context, null);
    }
    public FigureExGrid(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }
    public FigureExGrid(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
    }

    public void initView(int width, int height) {
        LayoutInflater.from(getContext()).inflate(R.layout.grid_figureex, this);

        mFigureImg = (ImageView) findViewById(R.id.figureex_grid_img);
        mDescText = (TextView) findViewById(R.id.figureex_grid_txt);
        mFigureInfoLy = (FrameLayout) findViewById(R.id.figureex_infoly);

        mDescText.setAlpha(0);
        setAlpha(0);

        mRootWidth = width;
        mRootHeight = height;
        mFigureImg.setLayoutParams(new LinearLayout.LayoutParams(mRootWidth, mRootWidth));

        setOnClickListener(this);
    }

    public void fillData(DLBridge bridge, CharacterEx character) {
        if(character == null)
            return;

        mBridge = bridge;
        mCharacterData = character;
        if(mCharacterData.actorPic != null)
            ImageLoader.getInstance(mContext).fillImage(mFigureImg, mCharacterData.actorPic, mRootWidth, mRootWidth);
        if(mCharacterData.talentList != null && mCharacterData.talentList.size() != 0) {
            List list = (List) mCharacterData.talentList;
            List<Character> characters = new ArrayList<>();
            Character cItem;
            for(Object obj : list) {
                cItem = new Character();
                LinkedHashMap<String, Object> lhm = (LinkedHashMap<String, Object>) obj;
                Iterator<String> keyIterator = lhm.keySet().iterator();
                while (keyIterator.hasNext()) {
                    String key = keyIterator.next();
                    Object value = lhm.get(key);

                    if(Character.ACTOR_NAME.equalsIgnoreCase(key)) {
                        cItem.actorName = (String) value;
                    } else if(Character.ACTOR_PIC.equalsIgnoreCase(key)) {
                        cItem.actorPic = (String) value;
                    } else if("actorDesc".equalsIgnoreCase(key)) {
                        cItem.actorDesc = (String) value;
                    } else if("guestName".equalsIgnoreCase(key)) {
                        cItem.actorName = (String) value;
                    } else if("guestPic".equalsIgnoreCase(key)) {
                        cItem.actorPic = (String) value;
                    } else if(Character.PRAISE_COUNT.equalsIgnoreCase(key)) {
                        cItem.praiseCount = (int) ((double) value);
                    } else if(Character.RELATED_ID.equalsIgnoreCase(key)) {
                        cItem.relatedId = (String) value;
                    } else if("guestDesc".equalsIgnoreCase(key)) {
                        cItem.actorDesc = (String) value;
                    } else if("praised".equalsIgnoreCase(key)) {
                        cItem.praised = (String) value;
                    } else if("period".equalsIgnoreCase(key)) {
                        cItem.period = (String) value;
                    } else if("isNotClick".equalsIgnoreCase(key)) {
                        cItem.isNotClick = (String) value;
                    }
                }
                characters.add(cItem);
            }

            if(characters.size() == 1) {
                mTeacherImg = new RoundedImageView(mContext);
                mTeacherImg.setCornerRadius(Utils.dip2px(mContext, 15));
                mTeacherImg.setBackground(mContext.getResources().getDrawable(R.drawable.bg_roundimg));
                mTeacherImg.setOval(true);
                mTeacherImg.setScaleType(ImageView.ScaleType.CENTER_CROP);
                mTeacherImg.setBorderWidth(2.0f);
                mTeacherImg.setBorderColor(Color.WHITE);
                ImageLoader.getInstance(mContext).fillImage(mTeacherImg, characters.get(0).actorPic, Utils.dip2px(mContext, 30), Utils.dip2px(mContext, 30));
                FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(Utils.dip2px(mContext, 30), Utils.dip2px(mContext, 30));
                layoutParams.gravity = Gravity.CENTER_VERTICAL|Gravity.RIGHT;
                layoutParams.rightMargin = Utils.dip2px(mContext, 10);
                mFigureInfoLy.addView(mTeacherImg, layoutParams);
            } else {
                mTeacherCountTxt = new TextView(mContext);
                mTeacherCountTxt.setTextSize(10);
                mTeacherCountTxt.setTextColor(Color.WHITE);
                mTeacherCountTxt.setText("+" + characters.size() + "\n...");
                mTeacherCountTxt.setLines(2);
                mTeacherCountTxt.setGravity(Gravity.CENTER);
                FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(Utils.dip2px(mContext, 15), ViewGroup.LayoutParams.WRAP_CONTENT);
                layoutParams.gravity = Gravity.CENTER_VERTICAL|Gravity.RIGHT;
                layoutParams.rightMargin = Utils.dip2px(mContext, 10);
                mFigureInfoLy.addView(mTeacherCountTxt, layoutParams);

                mTeacher1Img = new RoundedImageView(mContext);
                mTeacher1Img.setCornerRadius(Utils.dip2px(mContext, 30));
                mTeacher1Img.setBackground(mContext.getResources().getDrawable(R.drawable.bg_roundimg));
                mTeacher1Img.setAlpha(0.4f);
                mTeacher1Img.setOval(true);
                mTeacher1Img.setScaleType(ImageView.ScaleType.CENTER_CROP);
                mTeacher1Img.setBorderWidth(2.0f);
                mTeacher1Img.setBorderColor(Color.WHITE);
                ImageLoader.getInstance(mContext).fillImage(mTeacher1Img, characters.get(1).actorPic, Utils.dip2px(mContext, 30), Utils.dip2px(mContext, 30));
                layoutParams = new FrameLayout.LayoutParams(Utils.dip2px(mContext, 30), Utils.dip2px(mContext, 30));
                layoutParams.gravity = Gravity.CENTER_VERTICAL|Gravity.RIGHT;
                layoutParams.rightMargin = Utils.dip2px(mContext, 28);
                mFigureInfoLy.addView(mTeacher1Img, layoutParams);

                mTeacherImg = new RoundedImageView(mContext);
                mTeacherImg.setCornerRadius(Utils.dip2px(mContext, 30));
                mTeacherImg.setBackground(mContext.getResources().getDrawable(R.drawable.bg_roundimg));
                mTeacherImg.setOval(true);
                mTeacherImg.setScaleType(ImageView.ScaleType.CENTER_CROP);
                mTeacherImg.setBorderWidth(2.0f);
                mTeacherImg.setBorderColor(Color.WHITE);
                ImageLoader.getInstance(mContext).fillImage(mTeacherImg, characters.get(0).actorPic, Utils.dip2px(mContext, 30), Utils.dip2px(mContext, 30));
                layoutParams = new FrameLayout.LayoutParams(Utils.dip2px(mContext, 30), Utils.dip2px(mContext, 30));
                layoutParams.gravity = Gravity.CENTER_VERTICAL|Gravity.RIGHT;
                layoutParams.rightMargin = Utils.dip2px(mContext, 38);
                mFigureInfoLy.addView(mTeacherImg, layoutParams);
            }
        }

        mDescText.setText(mCharacterData.actorDesc);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        measureChild(getChildAt(0), MeasureSpec.makeMeasureSpec(mRootWidth, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(mRootHeight, MeasureSpec.EXACTLY));
        setMeasuredDimension(MeasureSpec.makeMeasureSpec(mRootWidth, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(mRootHeight, MeasureSpec.EXACTLY));
    }

    @Override
    public void onClick(View v) {
        DLIntent intent = new DLIntent("com.aube.plugin.origin", "com.aube.plugin.figure.CharacterActivity");
        intent.putExtra(CharacterActivity.ACTOR, mCharacterData);
        mBridge.openPluginActivity(intent);
    }

    public void startSlideInAnimComb(int delay) {
        ObjectAnimator imgAlpha = ObjectAnimator.ofFloat(this, "alpha", 0, 1f);
        imgAlpha.setDuration(300);
        imgAlpha.setStartDelay(delay);

        ObjectAnimator imgTranslate = ObjectAnimator.ofFloat(this, "translationX", mRootWidth/2, 0);
        imgTranslate.setDuration(300);
        imgAlpha.setStartDelay(delay);

        ObjectAnimator txtAlpha = ObjectAnimator.ofFloat(mDescText, "alpha", 0, 1f);
        txtAlpha.setDuration(300);
        txtAlpha.setStartDelay(200+delay);

        AnimatorSet mSet = new AnimatorSet();
        mSet.playTogether(imgAlpha, imgTranslate, txtAlpha);
        mSet.start();
    }
}
